﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;

public partial class Administration_CategoryNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack) { BindData(); }
        }
        catch (Exception ex)
        { Response.Write(ex.ToString()); }
        ContentPlaceHolder RightContentHolder = (ContentPlaceHolder)Master.FindControl("RightContentHolder");
        RightContentHolder.Visible = false;
    }

    private void BindData()
    {
        imgCateg.ImageUrl = Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_URL_PREFIX) + Constants.NO_IMAGE_FILE_NAME;
        htmlDescription.DesignPanelCssPath = Utils.GetApplicationSetting(Constants.STYLE_CSS_URL);
        htmlDescription.DocumentCssPath = Utils.GetApplicationSetting(Constants.STYLE_CSS_URL);

        // parent category dropdown binder
        DataTable dt = Entities.GetTreeCategories();
        ddlParentCateg.Items.Clear();
        int level = 0;
        RecursiveFillTree(dt, 0, level);
        ddlParentCateg.Items.Insert(0, new ListItem("Primul nivel", "0"));
    }
    private void RecursiveFillTree(DataTable dtParent, int parentID, int level)
    {
        level++; //on the each call level increment 1
        System.Text.StringBuilder appender = new System.Text.StringBuilder();

        for (int j = 0; j < level; j++)
        {
            appender.Append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|");
        }
        if (level > 0)
        {
            appender.Append("_ ");
        }

        DataView dv = new DataView(dtParent);

        dv.RowFilter = string.Format("pid = {0}", parentID);

        int i;

        if (dv.Count > 0)
        {
            for (i = 0; i < dv.Count; i++)
            {
                ddlParentCateg.Items.Add(new ListItem(Server.HtmlDecode(appender.ToString() + dv[i]["Title"].ToString()), dv[i]["id"].ToString()));
                RecursiveFillTree(dtParent, int.Parse(dv[i]["id"].ToString()), level);
            }
        }
        level--; //on the each function end level will decrement by 1
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        try
        {
            string imageFileName = uplImage.FileName;
            if (!string.IsNullOrEmpty(imageFileName))
            {
                uplImage.SaveAs(Utils.GetApplicationSetting(Constants.IMAGES_CATEGORIES_PHYSICAL_PATH) + uplImage.FileName);
            }

            int? newParentID = null;
            if (!string.IsNullOrEmpty(ddlParentCateg.SelectedItem.Value))
                newParentID = Convert.ToInt32(ddlParentCateg.SelectedValue);

            Categories newCateg = Entities.InsertOrUpdateCategory(null, txtTitle.Text, htmlDescription.Content, txtWebDesc.Text, txtKeys.Text, imageFileName, chApproved.Checked, chFeatured.Checked, newParentID, null, null);

            litMessage.Text = "Noua categorie a fost adaugata cu succes!";
            //BindData();
        }
        catch (Exception ex) { litMessage.Text = ex.ToString(); }
    }
}
